﻿using SimpleInjector;
using Sustainalytics.Claims;
using Sustainalytics.Entities;
using Sustainalytics.MongoDB.Extensions;
using Sustainalytics.Replication.Publisher;
using System.Collections.Generic;
using System.Configuration.Abstractions;
using System.Web.Http;

namespace Sustainalytics.Events.Service
{
    public static class CompositionRoot
    {
        public static Container RegisterDependencies(HttpConfiguration config)
        {
            var container = new Container();

            container.Options.DefaultScopedLifestyle = new SimpleInjector.Lifestyles.AsyncScopedLifestyle();
            container.RegisterWebApiControllers(config);

            container.RegisterCollections();

            container.RegisterMisc();

            container.Verify();
            return container;

        }

        public static void RegisterCollections(this Container container)
        {
            container.Register(() => MongoFactory.GetCollectionFromClientPlatform<Event>());
        }

        public static void RegisterMisc(this Container container)
        {
            container.RegisterSingleton(typeof(IClaimsChecker), () => new ClaimsChecker());
            container.RegisterSingleton(() =>
            {
                var serviceBusParams = new Dictionary<string, string>
                {
                        { "ConnectionString", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.ConnectionString"] },
                        { "RegionEndpoint", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.RegionEndpoint"] },
                        { "SessionId", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.SessionId"] },
                        { "TopicPath", ConfigurationManager.Instance.AppSettings["Microsoft.ServiceBus.TopicPath"] },
                };

                return new ServiceBusPublisher(serviceBusParams);
            });
        }
    }
}
